package com.hitqz.robot.biz.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hitqz.robot.api.business.dto.PatrolDevTree;
import com.hitqz.robot.api.business.entity.PatrolResultEntity;
import com.hitqz.robot.api.business.params.QueryPatrolSummaryParams;
import com.hitqz.robot.api.business.vo.PatrolResultSummaryVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * @author wengyx
 * @date 2024/8/11
 */
@Mapper
public interface PatrolResultMapper extends BaseMapper<PatrolResultEntity> {
    Page<PatrolResultSummaryVo> summaryPage(Page<PatrolResultSummaryVo> page, @Param(value = "query") QueryPatrolSummaryParams query);
    PatrolResultSummaryVo summary(@Param(value = "query")  QueryPatrolSummaryParams query);

    // 方法1：获取去重的设备路径
    @Select("SELECT  allot_robot_sn,dev_name FROM patrol_result pr " +
            "JOIN tickets t ON pr.tickets_id = t.id " +
            "WHERE  t.allot_robot_sn IN ('ZQ1', 'ZQ2') ")
    List<PatrolDevTree> selectAllDistinctDevPaths();

    // 方法2：获取设备下的检查项
    @Select("SELECT DISTINCT item_name FROM patrol_result pr " +
            "JOIN tickets t ON pr.tickets_id = t.id " +
            "WHERE  t.allot_robot_sn = #{robotSn} " +
            "AND  pr.dev_name =#{devPath}")
    List<String> selectItemsByDevPath(
            @Param("robotSn") String robotSn,
            @Param("devPath") String devPath);

    // 方法3：查询数值数据
    @Select("SELECT item_name,show_value, convert_value, check_time,vision_result FROM patrol_result pr " +
            "JOIN tickets t ON pr.tickets_id = t.id " +
            "WHERE t.allot_robot_sn = #{robotSn} " +
            "AND pr.dev_name = (#{devName}) " +
            "AND pr.item_name = (#{itemName}) " +
            "AND (pr.check_time BETWEEN #{start} AND #{end}) AND convert_value is not null order by check_time")
    List<PatrolResultEntity> selectItemValues(
            @Param("robotSn") String robotSn,
            @Param("devName") String devName,
            @Param("itemName") String itemName,
            @Param("start") Date startTime,
            @Param("end") Date endTime);

    @Select("SELECT DISTINCT(t.tickets_id) FROM patrol_result t WHERE t.stove_exec_no = (#{execNo})")
    List<Integer> selectTicketIdsByExecNo(@Param("execNo") String execNo);
}
